#用于操作流媒体服务器的方法


def post_Media(ip,num_id):#通知流媒体服务器
    url_ori='http://'+ip+':8080'
    headers = {'Content-type': 'application/json'}
    data={"secret": "035c73f7-bb6b-4889-a715-d9eb2d1925cc"}
    data_MediaList={"secret": "035c73f7-bb6b-4889-a715-d9eb2d1925cc","schema": "rtmp","app": "live"}
    startRecord='/index/api/startRecord'
    stopRecord='/index/api/stopRecord'
    getMp4RecordFile='/index/api/getMp4RecordFile'
    date_time=datetime.now()
    data_json = json.dumps(data)   
    url=url_ori+getMp4RecordFile+'?vhost=__defaultVhost__&app=live&stream=stream'+str(num_id)+'&period='+str(date_time.date())
    response = requests.post(url, data=data_json, headers=headers)
    res=response.json()
    mp4_name=''
    mp4_res=res['data']['paths'] #保存录制视频前mp4名字
    url=url_ori+startRecord+'?type=1&vhost=__defaultVhost__&app=live&stream=stream'+str(num_id)
    response = requests.post(url, data=data_json, headers=headers)
    time.sleep(120)#录制视频2min
    url=url_ori+stopRecord+'?type=1&vhost=__defaultVhost__&app=live&stream=stream'+str(num_id)
    response = requests.post(url, data=data_json, headers=headers)
    url=url_ori+getMp4RecordFile+'?vhost=__defaultVhost__&app=live&stream=stream'+str(num_id)+'&period='+str(date_time.date())
    response = requests.post(url, data=data_json, headers=headers)
    res=response.json()
    if len(res['data']['paths'])==len(mp4_res)+1: #如果录制视频数多了一个
        for re in res['data']['paths']:
           mp4_state=0
           mp4_rename=''
           for re1 in mp4_res:
               if re1==re:
                   mp4_state=1  #有相同名字的mp4
               else :
                   mp4_rename=res['data']['rootPath']+re
           if mp4_state==0:
               mp4_name=mp4_rename
    if mp4_name=='':  #排除因为接近24点，录制时间进入第二天
        date_time=datetime.now()
        url=url_ori+getMp4RecordFile+'?vhost=__defaultVhost__&app=live&stream=stream'+str(num_id)+'&period='+str(date_time.date())
        response = requests.post(url, data=data_json, headers=headers)
        res=response.json()
        if len(res['data']['paths'])==1:   #保存新一天的视频，数量应为1
            mp4_name=res['data']['paths'][0]
    if mp4_name=='':
        mp4_name=str(date_time)+'mp4 error'
    return mp4_name



